package com.newford.news.actions;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.newford.news.model.News;
import com.newford.news.service.NewsService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class NewsAddAction extends ActionSupport {

	private String newsid;
	private String title;

	private String content;
	private String userid;// 创建人ID
	private String href;
	private String delflag;// 0没有删除，1删除
	private Date commitdate;// 提交时间
	private Date updatedate;// 更新时间，用于排序
	private int readtime;// 阅读次数
	
	public String getNewsid() {
		return newsid;
	}

	public void setNewsid(String newsid) {
		this.newsid = newsid;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public String getDelflag() {
		return delflag;
	}

	public void setDelflag(String delflag) {
		this.delflag = delflag;
	}

	public Date getCommitdate() {
		return commitdate;
	}

	public void setCommitdate(Date commitdate) {
		this.commitdate = commitdate;
	}

	public Date getUpdatedate() {
		return updatedate;
	}

	public void setUpdatedate(Date updatedate) {
		this.updatedate = updatedate;
	}

	public int getReadtime() {
		return readtime;
	}

	public void setReadtime(int readtime) {
		this.readtime = readtime;
	}

	@Override
	public String execute() throws Exception {

		System.out.println(11);
		return super.execute();
	}
	
	
	public void addNews(){
		System.out.println(content);
		System.out.println(title);
//		WebApplicationContextUtils.getRequiredWebApplicationContext(ActionContext.getContext().);
		
		ApplicationContext context = new ClassPathXmlApplicationContext("../config/spring/applicationContext*.xml");
		
		NewsService newsService = (NewsService)context.getBean("inewsService");
		News news = new News();
		
		try {
//			news.setTitle(new String(title.getBytes("iso-8859-1"),"utf-8"));
			news.setTitle(title);
//			news.setTitle("阿德里开放时间了打开撒酒疯");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		news.setContent(content);
		newsService.addNews(news);
		
//		System.out.println(222);
	}
}
